public class Day10 {
    public static void main(String[] args) {
        String[] s = {"flower","flow","flight"};
        //longestCommonPrefix(s);
        String ss = new String();


    }
    //14.最长公共前缀
    public String longestCommonPrefix(String[] strs) {
        //1.找到最短的字符串
        int minLen = strs[0].length();
        String tmp = strs[0];
        for(int i=1;i<strs.length;i++) {
            if(strs[i].length() < minLen) {
                minLen = strs[i].length();
                tmp = strs[i];
            }
        }
        //2.寻找相同前缀
        String ret = tmp;
        for(int i=0;i<strs.length;i++) {
            int j = minLen;
            while(j>=0) {
                if(strs[i].substring(0,j).equals(tmp)) break;
                j--;
                tmp=tmp.substring(0,j);
            }
            minLen=j;
            if(minLen == 0) break;
        }
        return tmp;
    }
    public String longestCommonPrefix2(String[] strs) {
        String str = new String();
        int j = 0;
        while(true) {
            char tmp = 'a';
            if(j<strs[0].length()) {
                tmp = strs[0].charAt(j);
            }
            else break;
            int i=1;
            for(;i<strs.length;i++){
                if(j>=strs[i].length()) break;

                if(strs[i].charAt(j) != tmp) break;
            }
            if(i!=strs.length) break;
            else str+=tmp;
            j++;
        }
        return str;
    }
}
